home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr54 / bison118.zip / OUTPUT.C < prev    next >
C/C++ Source or Header  |  1993-06-01  |  28KB  |  1,425 lines

  1. /* Output the generated parsing program for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* functions to output parsing data to various files.  Entries are:
  22.  
  23.   output_headers ()
  24.  
  25. Output constant strings to the beginning of certain files.
  26.  
  27.   output_trailers()
  28.  
  29. Output constant strings to the ends of certain files.
  30.  
  31.   output ()
  32.  
  33. Output the parsing tables and the parser code to ftable.
  34.  
  35. The parser tables consist of these tables.
  36. Starred ones needed only for the semantic parser.
  37.  
  38. yytranslate = vector mapping yylex's token numbers into bison's token numbers.
  39.  
  40. yytname = vector of string-names indexed by bison token number
  41.  
  42. yyrline = vector of line-numbers of all rules.  For yydebug printouts.
  43.  
  44. yyrhs = vector of items of all rules.
  45.         This is exactly what ritems contains.  For yydebug and for semantic
  46.     parser.
  47.  
  48. yyprhs[r] = index in yyrhs of first item for rule r.
  49.  
  50. yyr1[r] = symbol number of symbol that rule r derives.
  51.  
  52. yyr2[r] = number of symbols composing right hand side of rule r.
  53.  
  54. * yystos[s] = the symbol number of the symbol that leads to state s.
  55.  
  56. yydefact[s] = default rule to reduce with in state s,
  57.           when yytable doesn't specify something else to do.
  58.           Zero means the default is an error.
  59.  
  60. yydefgoto[i] = default state to go to after a reduction of a rule that
  61.            generates variable ntokens + i, except when yytable
  62.            specifies something else to do.
  63.  
  64. yypact[s] = index in yytable of the portion describing state s.
  65.             The lookahead token's type is used to index that portion
  66.             to find out what to do.
  67.  
  68.         If the value in yytable is positive,
  69.         we shift the token and go to that state.
  70.  
  71.         If the value is negative, it is minus a rule number to reduce by.
  72.  
  73.         If the value is zero, the default action from yydefact[s] is used.
  74.  
  75. yypgoto[i] = the index in yytable of the portion describing 
  76.              what to do after reducing a rule that derives variable i + ntokens.
  77.              This portion is indexed by the parser state number
  78.          as of before the text for this nonterminal was read.
  79.          The value from yytable is the state to go to.
  80.  
  81. yytable = a vector filled with portions for different uses,
  82.           found via yypact and yypgoto.
  83.  
  84. yycheck = a vector indexed in parallel with yytable.
  85.       It indicates, in a roundabout way, the bounds of the
  86.       portion you are trying to examine.
  87.  
  88.       Suppose that the portion of yytable starts at index p
  89.       and the index to be examined within the portion is i.
  90.       Then if yycheck[p+i] != i, i is outside the bounds
  91.       of what is actually allocated, and the default
  92.       (from yydefact or yydefgoto) should be used.
  93.       Otherwise, yytable[p+i] should be used.
  94.  
  95. YYFINAL = the state number of the termination state.
  96. YYFLAG = most negative short int.  Used to flag ??
  97. YYNTBASE = ntokens.
  98.  
  99. */
  100.  
  101. #include <stdio.h>
  102. #include "system.h"
  103. #include "machine.h"
  104. #include "new.h"
  105. #include "files.h"
  106. #include "gram.h"
  107. #include "state.h"
  108.  
  109.  
  110. extern int debugflag;
  111. extern int nolinesflag;
  112.  
  113. extern char **tags;
  114. extern int tokensetsize;
  115. extern int final_state;
  116. extern core **state_table;
  117. extern shifts **shift_table;
  118. extern errs **err_table;
  119. extern reductions **reduction_table;
  120. extern short *accessing_symbol;
  121. extern unsigned *LA;
  122. extern short *LAruleno;
  123. extern short *lookaheads;
  124. extern char *consistent;
  125. extern short *goto_map;
  126. extern short *from_state;
  127. extern short *to_state;
  128.  
  129. void output_token_translations();
  130. void output_gram();
  131. void output_stos();
  132. void output_rule_data();
  133. void output_defines();
  134. void output_actions();
  135. void token_actions();
  136. void save_row();
  137. void goto_actions();
  138. void save_column();
  139. void sort_actions();
  140. void pack_table();
  141. void output_base();
  142. void output_table();
  143. void output_check();
  144. void output_parser();
  145. void output_program();
  146. void free_itemset();
  147. void free_shifts();
  148. void free_reductions();
  149. void free_itemsets();
  150. int action_row();
  151. int default_goto();
  152. int matching_state();
  153. int pack_vector();
  154.  
  155. extern void berror();
  156. extern void fatals();
  157.  
  158. static int nvectors;
  159. static int nentries;
  160. static short **froms;
  161. static short **tos;
  162. static short *tally;
  163. static short *width;
  164. static short *actrow;
  165. static short *state_count;
  166. static short *order;
  167. static short *base;
  168. static short *pos;
  169. static short *table;
  170. static short *check;
  171. static int lowzero;
  172. static int high;
  173.  
  174.  
  175.  
  176. #define    GUARDSTR    "\n#include \"%s\"\nextern int yyerror;\n\
  177. extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
  178. yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  179. register YYLTYPE *yylsp;\n\
  180. {\n  yyerror = 0;\nyycost = 0;\n  yymsg = 0;\nswitch (n)\n    {"
  181.  
  182. #define    ACTSTR        "\n#include \"%s\"\nextern YYSTYPE yyval;\
  183. \nextern int yychar;\
  184. yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  185. register YYLTYPE *yylsp;\n{\n  switch (n)\n{"
  186.  
  187. #define    ACTSTR_SIMPLE    "\n  switch (yyn) {\n"
  188.  
  189.  
  190. void
  191. output_headers()
  192. {
  193.   if (semantic_parser)
  194.     fprintf(fguard, GUARDSTR, attrsfile);
  195.   fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
  196. /*  if (semantic_parser)    JF moved this below
  197.     fprintf(ftable, "#include \"%s\"\n", attrsfile);
  198.   fprintf(ftable, "#include <stdio.h>\n\n");
  199. */
  200.  
  201.   /* Rename certain symbols if -p was specified.  */
  202.   if (spec_name_prefix)
  203.     {
  204.       fprintf(ftable, "#define yyparse %sparse\n", spec_name_prefix);
  205.       fprintf(ftable, "#define yylex %slex\n", spec_name_prefix);
  206.       fprintf(ftable, "#define yyerror %serror\n", spec_name_prefix);
  207.       fprintf(ftable, "#define yylval %slval\n", spec_name_prefix);
  208.       fprintf(ftable, "#define yychar %schar\n", spec_name_prefix);
  209.       fprintf(ftable, "#define yydebug %sdebug\n", spec_name_prefix);
  210.     }
  211. }
  212.  
  213.  
  214. void
  215. output_trailers()
  216. {
  217.   if (semantic_parser)
  218.     {
  219.       fprintf(fguard, "\n    }\n}\n");
  220.       fprintf(faction, "\n    }\n}\n");
  221.     }
  222.   else
  223.     fprintf(faction, "\n}\n");
  224. }
  225.  
  226.  
  227. void
  228. output()
  229. {
  230.   int c;
  231.  
  232.   /* output_token_defines(ftable);    / * JF put out token defines FIRST */
  233.   if (!semantic_parser)        /* JF Put out other stuff */
  234.     {
  235.       rewind(fattrs);
  236.       while ((c=getc(fattrs))!=EOF)
  237.         putc(c,ftable);
  238.     }
  239.  
  240.   if (debugflag)
  241.     fprintf(ftable, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n\n",
  242.         !!debugflag);
  243.  
  244.   if (semantic_parser)
  245.     fprintf(ftable, "#include \"%s\"\n", attrsfile);
  246.   fprintf(ftable, "#include <stdio.h>\n\n");
  247.  
  248.   /* Make "const" do nothing if not in ANSI C.  */
  249.   fprintf (ftable, "#ifndef __STDC__\n#define const\n#endif\n\n");
  250.  
  251.   free_itemsets();
  252.   output_defines();
  253.   output_token_translations();
  254. /*   if (semantic_parser) */
  255.   /* This is now unconditional because debugging printouts can use it.  */
  256.   output_gram();
  257.   FREE(ritem);
  258.   if (semantic_parser)
  259.     output_stos();
  260.   output_rule_data();
  261.   output_actions();
  262.   output_parser();
  263.   output_program();
  264. }
  265.  
  266.  
  267. void
  268. output_token_translations()
  269. {
  270.   register int i, j;
  271. /*   register short *sp; JF unused */
  272.  
  273.   if (translations)
  274.     {
  275.       fprintf(ftable,
  276.           "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
  277.           max_user_token_number, nsyms);
  278.     
  279.       if (ntokens < 127)  /* play it very safe; check maximum element value.  */
  280.         fprintf(ftable, "\nstatic const char yytranslate[] = {     0");
  281.       else
  282.     fprintf(ftable, "\nstatic const short yytranslate[] = {     0");
  283.     
  284.       j = 10;
  285.       for (i = 1; i <= max_user_token_number; i++)
  286.     {
  287.       putc(',', ftable);
  288.     
  289.       if (j >= 10)
  290.         {
  291.           putc('\n', ftable);
  292.           j = 1;
  293.         }
  294.       else
  295.         {
  296.           j++;
  297.         }
  298.     
  299.       fprintf(ftable, "%6d", token_translations[i]);
  300.     }
  301.     
  302.       fprintf(ftable, "\n};\n");
  303.     }
  304.   else
  305.     {
  306.       fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
  307.     } 
  308. }
  309.  
  310.  
  311. void
  312. output_gram()
  313. {
  314.   register int i;
  315.   register int j;
  316.   register short *sp;
  317.  
  318.   /* With the ordinary parser, yyprhs is needed only for yydebug.  */
  319.   if (!semantic_parser)
  320.     fprintf(ftable, "\n#if YYDEBUG != 0");
  321.  
  322.   fprintf(ftable, "\nstatic const short yyprhs[] = {     0");
  323.  
  324.   j = 10;
  325.   for (i = 1; i <= nrules; i++)
  326.     {
  327.       putc(',', ftable);
  328.  
  329.       if (j >= 10)
  330.     {
  331.       putc('\n', ftable);
  332.       j = 1;
  333.     }
  334.       else
  335.     {
  336.       j++;
  337.     }
  338.  
  339.       fprintf(ftable, "%6d", rrhs[i]);
  340.     }
  341.  
  342.   fprintf(ftable, "\n};\n");
  343.  
  344.   if(!semantic_parser)
  345.     fprintf(ftable, "\n#endif\n");
  346.  
  347.   fprintf(ftable, "\nstatic const short yyrhs[] = {%6d", ritem[0]);
  348.  
  349.   j = 10;
  350.   for (sp = ritem + 1; *sp; sp++)
  351.     {
  352.       putc(',', ftable);
  353.  
  354.       if (j >= 10)
  355.     {
  356.       putc('\n', ftable);
  357.       j = 1;
  358.     }
  359.       else
  360.     {
  361.       j++;
  362.     }
  363.  
  364.       if (*sp > 0)
  365.     fprintf(ftable, "%6d", *sp);
  366.       else
  367.     fprintf(ftable, "     0");
  368.     }
  369.  
  370.   fprintf(ftable, "\n};\n");
  371. }
  372.  
  373.  
  374. void
  375. output_stos()
  376. {
  377.   register int i;
  378.   register int j;
  379.  
  380.   fprintf(ftable, "\nstatic const short yystos[] = {     0");
  381.  
  382.   j = 10;
  383.   for (i = 1; i < nstates; i++)
  384.     {
  385.       putc(',', ftable);
  386.  
  387.       if (j >= 10)
  388.     {
  389.       putc('\n', ftable);
  390.       j = 1;
  391.     }
  392.       else
  393.     {
  394.       j++;
  395.     }
  396.  
  397.       fprintf(ftable, "%6d", accessing_symbol[i]);
  398.     }
  399.  
  400.   fprintf(ftable, "\n};\n");
  401. }
  402.  
  403.  
  404. void
  405. output_rule_data()
  406. {
  407.   register int i;
  408.   register int j;
  409.  
  410.   fprintf(ftable, "\n#if YYDEBUG != 0\nstatic const short yyrline[] = { 0");
  411.  
  412.   j = 10;
  413.   for (i = 1; i <= nrules; i++)
  414.     {
  415.       putc(',', ftable);
  416.  
  417.       if (j >= 10)
  418.     {
  419.       putc('\n', ftable);
  420.       j = 1;
  421.     }
  422.       else
  423.     {
  424.       j++;
  425.     }
  426.  
  427.       fprintf(ftable, "%6d", rline[i]);
  428.     }
  429.  
  430.   /* Output the table of symbol names.  */
  431.  
  432.   fprintf(ftable,
  433.           "\n};\n\nstatic const char * const yytname[] = {   \"%s\"",
  434.           tags[0]);
  435.  
  436.   j = strlen (tags[0]) + 44;
  437.   for (i = 1; i <= nsyms; i++)
  438.     {
  439.       register char *p;
  440.       putc(',', ftable);
  441.       j++;
  442.  
  443.       if (j > 75)
  444.     {
  445.       putc('\n', ftable);
  446.       j = 0;
  447.     }
  448.  
  449.       putc ('\"', ftable);
  450.       j++;
  451.  
  452.       for (p = tags[i]; p && *p; p++)
  453.     {
  454.       if (*p == '"' || *p == '\\')
  455.         {
  456.           fprintf(ftable, "\\%c", *p);
  457.           j += 2;
  458.         }
  459.       else if (*p == '\n')
  460.         {
  461.           fprintf(ftable, "\\n");
  462.           j += 2;
  463.         }
  464.       else if (*p == '\t')
  465.         {
  466.           fprintf(ftable, "\\t");
  467.           j += 2;
  468.         }
  469.       else if (*p == '\b')
  470.         {
  471.           fprintf(ftable, "\\b");
  472.           j += 2;
  473.         }
  474.       else if (*p < 040 || *p >= 0177)
  475.         {
  476.           fprintf(ftable, "\\%03o", *p);
  477.           j += 4;
  478.         }
  479.       else
  480.         {
  481.           putc(*p, ftable);
  482.           j++;
  483.         }
  484.     }
  485.  
  486.       putc ('\"', ftable);
  487.       j++;
  488.     }
  489.  
  490.   fprintf(ftable, "\n};\n#endif\n\nstatic const short yyr1[] = {     0");
  491.  
  492.   j = 10;
  493.   for (i = 1; i <= nrules; i++)
  494.     {
  495.       putc(',', ftable);
  496.  
  497.       if (j >= 10)
  498.     {
  499.       putc('\n', ftable);
  500.       j = 1;
  501.     }
  502.       else
  503.     {
  504.       j++;
  505.     }
  506.  
  507.       fprintf(ftable, "%6d", rlhs[i]);
  508.     }
  509.  
  510.   FREE(rlhs + 1);
  511.  
  512.   fprintf(ftable, "\n};\n\nstatic const short yyr2[] = {     0");
  513.  
  514.   j = 10;
  515.   for (i = 1; i < nrules; i++)
  516.     {
  517.       putc(',', ftable);
  518.  
  519.       if (j >= 10)
  520.     {
  521.       putc('\n', ftable);
  522.       j = 1;
  523.     }
  524.       else
  525.     {
  526.       j++;
  527.     }
  528.  
  529.       fprintf(ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1);
  530.     }
  531.  
  532.   putc(',', ftable);
  533.   if (j >= 10)
  534.     putc('\n', ftable);
  535.  
  536.   fprintf(ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
  537.   FREE(rrhs + 1);
  538. }
  539.  
  540.  
  541. void
  542. output_defines()
  543. {
  544.   fprintf(ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
  545.   fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT);
  546.   fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens);
  547. }
  548.  
  549.  
  550.  
  551. /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck.  */
  552.  
  553. void
  554. output_actions()
  555. {
  556.   nvectors = nstates + nvars;
  557.  
  558.   froms = NEW2(nvectors, short *);
  559.   tos = NEW2(nvectors, short *);
  560.   tally = NEW2(nvectors, short);
  561.   width = NEW2(nvectors, short);
  562.  
  563.   token_actions();
  564.   free_shifts();
  565.   free_reductions();
  566.   FREE(lookaheads);
  567.   FREE(LA);
  568.   FREE(LAruleno);
  569.   FREE(accessing_symbol);
  570.  
  571.   goto_actions();
  572.   FREE(goto_map + ntokens);
  573.   FREE(from_state);
  574.   FREE(to_state);
  575.  
  576.   sort_actions();
  577.   pack_table();
  578.   output_base();
  579.   output_table();
  580.   output_check();
  581. }
  582.  
  583.  
  584.  
  585. /* figure out the actions for the specified state, indexed by lookahead token type.
  586.  
  587.    The yydefact table is output now.  The detailed info
  588.    is saved for putting into yytable later.  */
  589.  
  590. void
  591. token_actions()
  592. {
  593.   register int i;
  594.   register int j;
  595.   register int k;
  596.  
  597.   actrow = NEW2(ntokens, short);
  598.  
  599.   k = action_row(0);
  600.   fprintf(ftable, "\nstatic const short yydefact[] = {%6d", k);
  601.   save_row(0);
  602.  
  603.   j = 10;
  604.   for (i = 1; i < nstates; i++)
  605.     {
  606.       putc(',', ftable);
  607.  
  608.       if (j >= 10)
  609.     {
  610.       putc('\n', ftable);
  611.       j = 1;
  612.     }
  613.       else
  614.     {
  615.       j++;
  616.     }
  617.  
  618.       k = action_row(i);
  619.       fprintf(ftable, "%6d", k);
  620.       save_row(i);
  621.     }
  622.  
  623.   fprintf(ftable, "\n};\n");
  624.   FREE(actrow);
  625. }
  626.  
  627.  
  628.  
  629. /* Decide what to do for each type of token if seen as the lookahead token in specified state.
  630.    The value returned is used as the default action (yydefact) for the state.
  631.    In addition, actrow is filled with what to do for each kind of token,
  632.    index by symbol number, with zero meaning do the default action.
  633.    The value MINSHORT, a very negative number, means this situation
  634.    is an error.  The parser recognizes this value specially.
  635.  
  636.    This is where conflicts are resolved.  The loop over lookahead rules
  637.    considered lower-numbered rules last, and the last rule considered that likes
  638.    a token gets to handle it.  */
  639.  
  640. int
  641. action_row(state)
  642. int state;
  643. {
  644.   register int i;
  645.   register int j;
  646.   register int k;
  647.   register int m;
  648.   register int n;
  649.   register int count;
  650.   register int default_rule;
  651.   register int nreds;
  652.   register int max;
  653.   register int rule;
  654.   register int shift_state;
  655.   register int symbol;
  656.   register unsigned mask;
  657.   register unsigned *wordp;
  658.   register reductions *redp;
  659.   register shifts *shiftp;
  660.   register errs *errp;
  661.   int nodefault = 0;  /* set nonzero to inhibit having any default reduction */
  662.  
  663.   for (i = 0; i < ntokens; i++)
  664.     actrow[i] = 0;
  665.  
  666.   default_rule = 0;
  667.   nreds = 0;
  668.   redp = reduction_table[state];
  669.  
  670.   if (redp)
  671.     {
  672.       nreds = redp->nreds;
  673.  
  674.       if (nreds >= 1)
  675.     {
  676.       /* loop over all the rules available here which require lookahead */
  677.       m = lookaheads[state];
  678.       n = lookaheads[state + 1];
  679.  
  680.       for (i = n - 1; i >= m; i--)
  681.         {
  682.           rule = - LAruleno[i];
  683.           wordp = LA + i * tokensetsize;
  684.           mask = 1;
  685.  
  686.           /* and find each token which the rule finds acceptable to come next */
  687.           for (j = 0; j < ntokens; j++)
  688.         {
  689.           /* and record this rule as the rule to use if that token follows.  */
  690.           if (mask & *wordp)
  691.             actrow[j] = rule;
  692.  
  693.           mask <<= 1;
  694.           if (mask == 0)
  695.             {
  696.               mask = 1;
  697.               wordp++;
  698.             }
  699.         }
  700.         }
  701.     }
  702.     }
  703.  
  704.   shiftp = shift_table[state];
  705.  
  706.   /* now see which tokens are allowed for shifts in this state.
  707.      For them, record the shift as the thing to do.  So shift is preferred to reduce.  */
  708.  
  709.   if (shiftp)
  710.     {
  711.       k = shiftp->nshifts;
  712.  
  713.       for (i = 0; i < k; i++)
  714.     {
  715.       shift_state = shiftp->shifts[i];
  716.       if (! shift_state) continue;
  717.  
  718.       symbol = accessing_symbol[shift_state];
  719.  
  720.       if (ISVAR(symbol))
  721.         break;
  722.  
  723.       actrow[symbol] = shift_state;
  724.  
  725.       /* do not use any default reduction if there is a shift for error */
  726.  
  727.       if (symbol == error_token_number) nodefault = 1;
  728.     }
  729.     }
  730.  
  731.   errp = err_table[state];
  732.  
  733.   /* See which tokens are an explicit error in this state
  734.      (due to %nonassoc).  For them, record MINSHORT as the action.  */
  735.  
  736.   if (errp)
  737.     {
  738.       k = errp->nerrs;
  739.  
  740.       for (i = 0; i < k; i++)
  741.     {
  742.       symbol = errp->errs[i];
  743.       actrow[symbol] = MINSHORT;
  744.     }
  745.     }
  746.  
  747.   /* now find the most common reduction and make it the default action for this state.  */
  748.  
  749.   if (nreds >= 1 && ! nodefault)
  750.     {
  751.       if (consistent[state])
  752.     default_rule = redp->rules[0];
  753.       else
  754.     {
  755.       max = 0;
  756.       for (i = m; i < n; i++)
  757.         {
  758.           count = 0;
  759.           rule = - LAruleno[i];
  760.     
  761.           for (j = 0; j < ntokens; j++)
  762.         {
  763.           if (actrow[j] == rule)
  764.             count++;
  765.         }
  766.     
  767.           if (count > max)
  768.         {
  769.           max = count;
  770.           default_rule = rule;
  771.         }
  772.         }
  773.     
  774.       /* actions which match the default are replaced with zero,
  775.          which means "use the default" */
  776.     
  777.       if (max > 0)
  778.         {
  779.           for (j = 0; j < ntokens; j++)
  780.         {
  781.           if (actrow[j] == default_rule)
  782.             actrow[j] = 0;
  783.         }
  784.     
  785.           default_rule = - default_rule;
  786.         }
  787.     }
  788.     }
  789.  
  790.   /* If have no default rule, the default is an error.
  791.      So replace any action which says "error" with "use default".  */
  792.  
  793.   if (default_rule == 0)
  794.     for (j = 0; j < ntokens; j++)
  795.       {
  796.     if (actrow[j] == MINSHORT)
  797.       actrow[j] = 0;
  798.       }
  799.  
  800.   return (default_rule);
  801. }
  802.  
  803.  
  804. void
  805. save_row(state)
  806. int state;
  807. {
  808.   register int i;
  809.   register int count;
  810.   register short *sp;
  811.   register short *sp1;
  812.   register short *sp2;
  813.  
  814.   count = 0;
  815.   for (i = 0; i < ntokens; i++)
  816.     {
  817.       if (actrow[i] != 0)
  818.     count++;
  819.     }
  820.  
  821.   if (count == 0)
  822.     return;
  823.  
  824.   froms[state] = sp1 = sp = NEW2(count, short);
  825.   tos[state] = sp2 = NEW2(count, short);
  826.  
  827.   for (i = 0; i < ntokens; i++)
  828.     {
  829.       if (actrow[i] != 0)
  830.     {
  831.       *sp1++ = i;
  832.       *sp2++ = actrow[i];
  833.     }
  834.     }
  835.  
  836.   tally[state] = count;
  837.   width[state] = sp1[-1] - sp[0] + 1;
  838. }
  839.  
  840.  
  841.  
  842. /* figure out what to do after reducing with each rule,
  843.    depending on the saved state from before the beginning
  844.    of parsing the data that matched this rule.
  845.  
  846.    The yydefgoto table is output now.  The detailed info
  847.    is saved for putting into yytable later.  */
  848.  
  849. void
  850. goto_actions()
  851. {
  852.   register int i;
  853.   register int j;
  854.   register int k;
  855.  
  856.   state_count = NEW2(nstates, short);
  857.  
  858.   k = default_goto(ntokens);
  859.   fprintf(ftable, "\nstatic const short yydefgoto[] = {%6d", k);
  860.   save_column(ntokens, k);
  861.  
  862.   j = 10;
  863.   for (i = ntokens + 1; i < nsyms; i++)
  864.     {
  865.       putc(',', ftable);
  866.  
  867.       if (j >= 10)
  868.     {
  869.       putc('\n', ftable);
  870.       j = 1;
  871.     }
  872.       else
  873.     {
  874.       j++;
  875.     }
  876.  
  877.       k = default_goto(i);
  878.       fprintf(ftable, "%6d", k);
  879.       save_column(i, k);
  880.     }
  881.  
  882.   fprintf(ftable, "\n};\n");
  883.   FREE(state_count);
  884. }
  885.  
  886.  
  887.  
  888. int
  889. default_goto(symbol)
  890. int symbol;
  891. {
  892.   register int i;
  893.   register int m;
  894.   register int n;
  895.   register int default_state;
  896.   register int max;
  897.  
  898.   m = goto_map[symbol];
  899.   n = goto_map[symbol + 1];
  900.  
  901.   if (m == n)
  902.     return (-1);
  903.  
  904.   for (i = 0; i < nstates; i++)
  905.     state_count[i] = 0;
  906.  
  907.   for (i = m; i < n; i++)
  908.     state_count[to_state[i]]++;
  909.  
  910.   max = 0;
  911.   default_state = -1;
  912.  
  913.   for (i = 0; i < nstates; i++)
  914.     {
  915.       if (state_count[i] > max)
  916.     {
  917.       max = state_count[i];
  918.       default_state = i;
  919.     }
  920.     }
  921.  
  922.   return (default_state);
  923. }
  924.  
  925.  
  926. void
  927. save_column(symbol, default_state)
  928. int symbol;
  929. int default_state;
  930. {
  931.   register int i;
  932.   register int m;
  933.   register int n;
  934.   register short *sp;
  935.   register short *sp1;
  936.   register short *sp2;
  937.   register int count;
  938.   register int symno;
  939.  
  940.   m = goto_map[symbol];
  941.   n = goto_map[symbol + 1];
  942.  
  943.   count = 0;
  944.   for (i = m; i < n; i++)
  945.     {
  946.       if (to_state[i] != default_state)
  947.     count++;
  948.     }
  949.  
  950.   if (count == 0)
  951.     return;
  952.  
  953.   symno = symbol - ntokens + nstates;
  954.  
  955.   froms[symno] = sp1 = sp = NEW2(count, short);
  956.   tos[symno] = sp2 = NEW2(count, short);
  957.  
  958.   for (i = m; i < n; i++)
  959.     {
  960.       if (to_state[i] != default_state)
  961.     {
  962.       *sp1++ = from_state[i];
  963.       *sp2++ = to_state[i];
  964.     }
  965.     }
  966.  
  967.   tally[symno] = count;
  968.   width[symno] = sp1[-1] - sp[0] + 1;
  969. }
  970.  
  971.  
  972.  
  973. /* the next few functions decide how to pack 
  974.    the actions and gotos information into yytable. */
  975.  
  976. void
  977. sort_actions()
  978. {
  979.   register int i;
  980.   register int j;
  981.   register int k;
  982.   register int t;
  983.   register int w;
  984.  
  985.   order = NEW2(nvectors, short);
  986.   nentries = 0;
  987.  
  988.   for (i = 0; i < nvectors; i++)
  989.     {
  990.       if (tally[i] > 0)
  991.     {
  992.       t = tally[i];
  993.       w = width[i];
  994.       j = nentries - 1;
  995.  
  996.       while (j >= 0 && (width[order[j]] < w))
  997.         j--;
  998.  
  999.       while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
  1000.         j--;
  1001.  
  1002.       for (k = nentries - 1; k > j; k--)
  1003.         order[k + 1] = order[k];
  1004.  
  1005.       order[j + 1] = i;
  1006.       nentries++;
  1007.     }
  1008.     }
  1009. }
  1010.  
  1011.  
  1012. void
  1013. pack_table()
  1014. {
  1015.   register int i;
  1016.   register int place;
  1017.   register int state;
  1018.  
  1019.   base = NEW2(nvectors, short);
  1020.   pos = NEW2(nentries, short);
  1021.   table = NEW2(MAXTABLE, short);
  1022.   check = NEW2(MAXTABLE, short);
  1023.  
  1024.   lowzero = 0;
  1025.   high = 0;
  1026.  
  1027.   for (i = 0; i < nvectors; i++)
  1028.     base[i] = MINSHORT;
  1029.  
  1030.   for (i = 0; i < MAXTABLE; i++)
  1031.     check[i] = -1;
  1032.  
  1033.   for (i = 0; i < nentries; i++)
  1034.     {
  1035.       state = matching_state(i);
  1036.  
  1037.       if (state < 0)
  1038.     place = pack_vector(i);
  1039.       else
  1040.     place = base[state];
  1041.  
  1042.       pos[i] = place;
  1043.       base[order[i]] = place;
  1044.     }
  1045.  
  1046.   for (i = 0; i < nvectors; i++)
  1047.     {
  1048.       if (froms[i])
  1049.     FREE(froms[i]);
  1050.       if (tos[i])
  1051.     FREE(tos[i]);
  1052.     }
  1053.  
  1054.   FREE(froms);
  1055.   FREE(tos);
  1056.   FREE(pos);
  1057. }
  1058.  
  1059.  
  1060.  
  1061. int
  1062. matching_state(vector)
  1063. int vector;
  1064. {
  1065.   register int i;
  1066.   register int j;
  1067.   register int k;
  1068.   register int t;
  1069.   register int w;
  1070.   register int match;
  1071.   register int prev;
  1072.  
  1073.   i = order[vector];
  1074.   if (i >= nstates)
  1075.     return (-1);
  1076.  
  1077.   t = tally[i];
  1078.   w = width[i];
  1079.  
  1080.   for (prev = vector - 1; prev >= 0; prev--)
  1081.     {
  1082.       j = order[prev];
  1083.       if (width[j] != w || tally[j] != t)
  1084.     return (-1);
  1085.  
  1086.       match = 1;
  1087.       for (k = 0; match && k < t; k++)
  1088.     {
  1089.       if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
  1090.         match = 0;
  1091.     }
  1092.  
  1093.       if (match)
  1094.     return (j);
  1095.     }
  1096.  
  1097.   return (-1);
  1098. }
  1099.  
  1100.  
  1101.  
  1102. int
  1103. pack_vector(vector)
  1104. int vector;
  1105. {
  1106.   register int i;
  1107.   register int j;
  1108.   register int k;
  1109.   register int t;
  1110.   register int loc;
  1111.   register int ok;
  1112.   register short *from;
  1113.   register short *to;
  1114.  
  1115.   i = order[vector];
  1116.   t = tally[i];
  1117.  
  1118.   if (t == 0)
  1119.     berror("pack_vector");
  1120.  
  1121.   from = froms[i];
  1122.   to = tos[i];
  1123.  
  1124.   for (j = lowzero - from[0]; j < MAXTABLE; j++)
  1125.     {
  1126.       ok = 1;
  1127.  
  1128.       for (k = 0; ok && k < t; k++)
  1129.     {
  1130.       loc = j + from[k];
  1131.       if (loc > MAXTABLE)
  1132.         fatals("maximum table size (%d) exceeded",MAXTABLE);
  1133.  
  1134.       if (table[loc] != 0)
  1135.         ok = 0;
  1136.     }
  1137.  
  1138.       for (k = 0; ok && k < vector; k++)
  1139.     {
  1140.       if (pos[k] == j)
  1141.         ok = 0;
  1142.     }
  1143.  
  1144.       if (ok)
  1145.     {
  1146.       for (k = 0; k < t; k++)
  1147.         {
  1148.           loc = j + from[k];
  1149.           table[loc] = to[k];
  1150.           check[loc] = from[k];
  1151.         }
  1152.  
  1153.       while (table[lowzero] != 0)
  1154.         lowzero++;
  1155.  
  1156.       if (loc > high)
  1157.         high = loc;
  1158.  
  1159.       return (j);
  1160.     }
  1161.     }
  1162.  
  1163.   berror("pack_vector");
  1164.   return 0;    /* JF keep lint happy */
  1165. }
  1166.  
  1167.  
  1168.  
  1169. /* the following functions output yytable, yycheck
  1170.    and the vectors whose elements index the portion starts */
  1171.  
  1172. void
  1173. output_base()
  1174. {
  1175.   register int i;
  1176.   register int j;
  1177.  
  1178.   fprintf(ftable, "\nstatic const short yypact[] = {%6d", base[0]);
  1179.  
  1180.   j = 10;
  1181.   for (i = 1; i < nstates; i++)
  1182.     {
  1183.       putc(',', ftable);
  1184.  
  1185.       if (j >= 10)
  1186.     {
  1187.       putc('\n', ftable);
  1188.       j = 1;
  1189.     }
  1190.       else
  1191.     {
  1192.       j++;
  1193.     }
  1194.  
  1195.       fprintf(ftable, "%6d", base[i]);
  1196.     }
  1197.  
  1198.   fprintf(ftable, "\n};\n\nstatic const short yypgoto[] = {%6d", base[nstates]);
  1199.  
  1200.   j = 10;
  1201.   for (i = nstates + 1; i < nvectors; i++)
  1202.     {
  1203.       putc(',', ftable);
  1204.  
  1205.       if (j >= 10)
  1206.     {
  1207.       putc('\n', ftable);
  1208.       j = 1;
  1209.     }
  1210.       else
  1211.     {
  1212.       j++;
  1213.     }
  1214.  
  1215.       fprintf(ftable, "%6d", base[i]);
  1216.     }
  1217.  
  1218.   fprintf(ftable, "\n};\n");
  1219.   FREE(base);
  1220. }
  1221.  
  1222.  
  1223. void
  1224. output_table()
  1225. {
  1226.   register int i;
  1227.   register int j;
  1228.  
  1229.   fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
  1230.   fprintf(ftable, "\nstatic const short yytable[] = {%6d", table[0]);
  1231.  
  1232.   j = 10;
  1233.   for (i = 1; i <= high; i++)
  1234.     {
  1235.       putc(',', ftable);
  1236.  
  1237.       if (j >= 10)
  1238.     {
  1239.       putc('\n', ftable);
  1240.       j = 1;
  1241.     }
  1242.       else
  1243.     {
  1244.       j++;
  1245.     }
  1246.  
  1247.       fprintf(ftable, "%6d", table[i]);
  1248.     }
  1249.  
  1250.   fprintf(ftable, "\n};\n");
  1251.   FREE(table);
  1252. }
  1253.  
  1254.  
  1255. void
  1256. output_check()
  1257. {
  1258.   register int i;
  1259.   register int j;
  1260.  
  1261.   fprintf(ftable, "\nstatic const short yycheck[] = {%6d", check[0]);
  1262.  
  1263.   j = 10;
  1264.   for (i = 1; i <= high; i++)
  1265.     {
  1266.       putc(',', ftable);
  1267.  
  1268.       if (j >= 10)
  1269.     {
  1270.       putc('\n', ftable);
  1271.       j = 1;
  1272.     }
  1273.       else
  1274.     {
  1275.       j++;
  1276.     }
  1277.  
  1278.       fprintf(ftable, "%6d", check[i]);
  1279.     }
  1280.  
  1281.   fprintf(ftable, "\n};\n");
  1282.   FREE(check);
  1283. }
  1284.  
  1285.  
  1286.  
  1287. /* copy the parser code into the ftable file at the end.  */
  1288.  
  1289. void
  1290. output_parser()
  1291. {
  1292.   register int c;
  1293. #ifdef DONTDEF
  1294.   FILE *fpars;
  1295. #else
  1296. #define fpars fparser
  1297. #endif
  1298.  
  1299.   if (pure_parser)
  1300.     fprintf(ftable, "#define YYPURE 1\n\n");
  1301.  
  1302. #ifdef DONTDEF    /* JF no longer needed 'cuz open_extra_files changes the
  1303.            currently open parser from bison.simple to bison.hairy */
  1304.   if (semantic_parser)
  1305.     fpars = fparser;
  1306.   else fpars = fparser1;
  1307. #endif
  1308.  
  1309.   /* Loop over lines in the standard parser file.  */
  1310.  
  1311.   while (1)
  1312.     {
  1313.       int write_line = 1;
  1314.  
  1315.       c = getc(fpars);
  1316.  
  1317.       /* See if the line starts with `#line.
  1318.      If so, set write_line to 0.  */
  1319.       if (nolinesflag)
  1320.     if (c == '#') 
  1321.       {
  1322.         c = getc(fpars);
  1323.         if (c == 'l')
  1324.           {
  1325.         c = getc(fpars);
  1326.         if (c == 'i')
  1327.           {
  1328.             c = getc(fpars);
  1329.             if (c == 'n')
  1330.               {
  1331.             c = getc(fpars);
  1332.             if (c == 'e')
  1333.               write_line = 0;
  1334.             else
  1335.               fprintf(ftable, "#lin");
  1336.               }
  1337.             else
  1338.               fprintf(ftable, "#li");
  1339.           }
  1340.         else
  1341.           fprintf(ftable, "#l");
  1342.           }
  1343.         else
  1344.           fprintf(ftable, "#");
  1345.       }
  1346.  
  1347.       /* now write out the line... */
  1348.       for ( ; c != '\n' && c != EOF; c = getc(fpars))
  1349.     if (write_line)
  1350.       if (c == '$')
  1351.         {
  1352.           /* `$' in the parser file indicates where to put the actions.
  1353.          Copy them in at this point.  */
  1354.           rewind(faction);
  1355.           for(c=getc(faction);c!=EOF;c=getc(faction))
  1356.         putc(c,ftable);
  1357.         }
  1358.       else
  1359.         putc(c, ftable);
  1360.       if (c == EOF)
  1361.     break;
  1362.       putc(c, ftable);
  1363.     }
  1364. }
  1365.  
  1366.  
  1367. void
  1368. output_program()
  1369. {
  1370.   register int c;
  1371.   extern int lineno;
  1372.  
  1373.   if (!nolinesflag)
  1374.     fprintf(ftable, "#line %d \"%s\"\n", lineno, infile);
  1375.  
  1376.   c = getc(finput);
  1377.   while (c != EOF)
  1378.     {
  1379.       putc(c, ftable);
  1380.       c = getc(finput);
  1381.     }
  1382. }
  1383.  
  1384.  
  1385. void
  1386. free_itemsets()
  1387. {
  1388.   register core *cp,*cptmp;
  1389.  
  1390.   FREE(state_table);
  1391.  
  1392.   for (cp = first_state; cp; cp = cptmp) {
  1393.     cptmp=cp->next;
  1394.     FREE(cp);
  1395.   }
  1396. }
  1397.  
  1398.  
  1399. void
  1400. free_shifts()
  1401. {
  1402.   register shifts *sp,*sptmp;/* JF derefrenced freed ptr */
  1403.  
  1404.   FREE(shift_table);
  1405.  
  1406.   for (sp = first_shift; sp; sp = sptmp) {
  1407.     sptmp=sp->next;
  1408.     FREE(sp);
  1409.   }
  1410. }
  1411.  
  1412.  
  1413. void
  1414. free_reductions()
  1415. {
  1416.   register reductions *rp,*rptmp;/* JF fixed freed ptr */
  1417.  
  1418.   FREE(reduction_table);
  1419.  
  1420.   for (rp = first_reduction; rp; rp = rptmp) {
  1421.     rptmp=rp->next;
  1422.     FREE(rp);
  1423.   }
  1424. }
  1425.